package com.itbooking.service.customer;


import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itbooking.mapper.CustomerMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbooking.pojo.Customer;
import com.itbooking.core.response.ServerResponse;
import com.itbooking.core.Params;
import org.springframework.util.StringUtils;

/**
 * 
 * todo:客户管理Service实现类
 * CustomerServiceImpl<br/>
 * 作者:黄佳钦大佬<br/>
 * 创建时间：2020年04月26日 23:48:11 <br/>
 * @version 1.0.0<br/>
 *
 */
@Service
public class CustomerServiceImpl implements ICustomerService  {

	@Autowired
	private CustomerMapper customerMapper;

	@Override
	public ServerResponse queryCustomerAll(String keyword,int pageNo,int pageSize){
		Page<Customer> page = new Page<>(pageNo,pageSize);
		Params params = new Params();
		params.setIsDelete(0);
		params.setKeyword(keyword);
		List<Customer> results = customerMapper.queryCustomerAll(params);
		page.setRecords(results);
		return ServerResponse.createBySuccess(page);
	}

	@Override
	public ServerResponse queryCustomerAllForMap(String keyword,int pageNo,int pageSize){
		Page<Map<String,Object>> page = new Page<>(pageNo,pageSize);
		Params params = new Params();
		params.setIsDelete(0);
		params.setKeyword(keyword);
		List<Map<String,Object>>  results = customerMapper.queryCustomerAllForMap(params);
		page.setRecords(results);
		return ServerResponse.createBySuccess(page);
	}

	@Override
	public ServerResponse getCustomerById(Integer id) {
	Customer customer = customerMapper.getCustomerById(id);
	return customer!=null?ServerResponse.createBySuccess(customer):ServerResponse.createByError();
	}

	@Override
	public ServerResponse getCustomerByIdForMap(Integer id){
	Map<String,Object> customer = customerMapper.getCustomerByIdForMap(id);
	return customer!=null?ServerResponse.createBySuccess(customer):ServerResponse.createByError();
	}
	
	@Override
	public ServerResponse saveCustomer(Customer customer) {
		// 客户id
		customer.setCustomerId(null);
		// 
		customer.setFirstName(null);
		// 
		customer.setLastName(null);
		// 0-女,1-男
		customer.setGender(null);
		// 
		customer.setEmail(null);
		// 
		customer.setPassword(null);
	    // 删除状态 0未删除 1删除
		customer.setIsDeleted(0);
		// 发布状态 0未发布 1发布
		customer.setStatus(1);
		// 
		customer.setVersion(null);
		int count = customerMapper.saveCustomer(customer);
		return count>0?ServerResponse.createBySuccess(customer):ServerResponse.createByError();
	}
	
	@Override
	public ServerResponse updateCustomer(Customer customer) {
		int count = customerMapper.updateCustomer(customer);
		return count>0?ServerResponse.createBySuccess(customer):ServerResponse.createByError();
	}

	@Override
	public ServerResponse deleteCustomerById(Integer id) {
		int count = customerMapper.deleteCustomerById(id);
		return count>0?ServerResponse.createBySuccess():ServerResponse.createByError();
	}

	@Override
	public ServerResponse deleteCustomerByIds(String opids) {
		if(StringUtils.isEmpty(opids)){return ServerResponse.createByError();}
		String[] idStrings = opids.split(",");
		List<Integer> idIntegers = new ArrayList<Integer>();
		for (String string : idStrings) {
			idIntegers.add(Integer.parseInt(string));
		}
		int count = customerMapper.deleteCustomerByIds(idIntegers);
		return count>0?ServerResponse.createBySuccess(count):ServerResponse.createByError();
	}
}
